package com.lvyq.myplayer.model.common

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.lvyq.myplayer.common.shared.MainAudioSharedPrefsManager
import com.lvyq.myplayer.parcelable.EventAudioChangeState
import com.lvyq.myplayer.parcelable.EventPlayState
import com.lvyq.myplayer.parcelable.SongInfoData

/**
 * 文件描述：EventPlayerViewModel  播放状态统一管理
 * 作者：lvyq
 * 创建时间：2025-06-27
 * 修改时间：2025-06-27
 */
class EventPlayerViewModel: ViewModel() {
    //播放状态

    val eventPlayState: LiveData<EventPlayState> = _eventPlayState
    val eventPlaySong: LiveData<SongInfoData> = _eventPlaySong
    val eventCurSongId: LiveData<EventAudioChangeState> = _eventCurSongId
    init {
        //默认未播放
        _eventPlayState.value = EventPlayState(playState = true)
    }

    companion object {

        // 初始化默认状态
        private val _eventPlayState = MutableLiveData<EventPlayState>()
        private val _eventPlaySong = MutableLiveData<SongInfoData>()
        private val _eventCurSongId = MutableLiveData<EventAudioChangeState>()
        // 更新播放状态的方法
        fun updatePlayState(newState: Boolean) {
            val newEventState = _eventPlayState.value?.copy(playState = newState)
                ?: EventPlayState(newState)

            // 更新 LiveData 值（会自动触发观察者）
            _eventPlayState.value = newEventState
        }

        fun updateCurrentPlaySong(data: SongInfoData) {
            val newEventState =  data.copy()

            // 更新 LiveData 值（会自动触发观察者）
            _eventPlaySong.value = newEventState
        }

        /**
         * 更新当前正在播放的id，用于更新页面的播放图标
         */
        fun updateCurrentPlaySongId(curPosition: Int) {
            val newEventState = _eventCurSongId.value?.copy(ponsiton = curPosition)
                ?: EventAudioChangeState(curPosition)

            // 更新 LiveData 值（会自动触发观察者）
            _eventCurSongId.value = newEventState
        }
    }


}